# encoding: utf-8
# @File  : spider.py
# @Author: shaoyun
# @Date  : 2025/05/11
from datetime import datetime
from typing import Optional

from pydantic import BaseModel, ConfigDict


class SpiderBase(BaseModel):
    """爬虫脚本基本信息"""
    name: str
    description: Optional[str] = None
    language: str = "python"  # 默认为Python

class SpiderCreate(SpiderBase):
    """创建爬虫模型"""

class SpiderUpdate(BaseModel):
    """更新爬虫脚本的请求模型"""
    name: Optional[str] = None
    description: Optional[str] = None


class SpiderInDB(SpiderBase):
    """数据库中的爬虫脚本模型"""
    id: str
    user_id: str
    path: str
    file_id: Optional[str] = None  # GridFS文件ID（新版本）
    version: str
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)  # 替代 orm_mode


class SpiderWithStats(SpiderInDB):
    """包含统计信息的爬虫脚本模型"""
    run_count: int = 0
    success_count: int = 0
    failure_count: int = 0
    average_runtime: float = 0.0